package com.gy.api.pc.sys.role.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.pagehelper.PageInfo;
import com.gy.api.base.*;
import com.gy.api.pc.sys.authority.domain.vo.SysAuthoritySearchVO;
import com.gy.api.pc.sys.authority.domain.vo.SysAuthorityTreeVO;
import com.gy.api.pc.sys.authority.domain.vo.SysAuthorityVO;
import com.gy.api.pc.sys.authority.service.ISysAuthorityService;
import com.gy.api.pc.sys.role.domain.vo.*;
import com.gy.api.pc.sys.role.service.ISysRoleService;
import com.gy.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.SysRoleController)
@RequestMapping(value = "/sys/role")
@RestController
@SaCheckLogin
public class SysRoleController {

    @Autowired
    private ISysRoleService service;

    @Autowired
    private ISysAuthorityService sysAuthorityService;

    @ApiOperation(value = "角色_查询列表")
    @GetMapping(value = "/role_list")
    public R<RestPage<List<SysRoleVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute SysRoleSearchVO searchVO) {
        List<SysRoleVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "角色_创建")
    @PostMapping(value = "/role_add")
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody SysRoleAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "角色_删除")
    @PostMapping(value = "/role_delete")
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }


    @ApiOperation(value = "角色_批量删除")
    @PostMapping(value = "/role_delete_batch")
    @SaCheckLogin
    public R deleteBath( @ApiParam(value = "修改参数") @Valid @RequestBody IDsEntity ids) {
        service.deleteBath(ids.getIds());
        return new R();
    }


    @ApiOperation(value = "角色_修改")
    @PostMapping(value = "/role_update")
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody SysRoleUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }

    @ApiOperation(value = "权限_查询角色拥有")
    @GetMapping(value = "/get_authority_list")
    public R<List<SysAuthorityVO>> getRoleAuthorityList(@ApiParam(value = "角色ID") @ModelAttribute  IDEntity idEntity) {
        SysAuthoritySearchVO searchVO=new SysAuthoritySearchVO();
        searchVO.setRoleId(idEntity.getId());
        List<SysAuthorityVO> list = sysAuthorityService.getList(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "权限_查询所有系统权限(树形)")
    @GetMapping(value = "/get_authority_tree")
    public R<List<SysAuthorityTreeVO>> authority_tree() {
        SysAuthoritySearchVO searchVO=new SysAuthoritySearchVO();
        List<SysAuthorityTreeVO> list = sysAuthorityService.getTreeList(searchVO);
        return new R(list);
    }

}
